/*****************************************************************************
 * main_window.c : GTK+ plugin for vlc
 *****************************************************************************/

#include <gtk/gtk.h>

#include "intf.h"
#include "vlc_connect.h"

#include "menubar.h"
#include "components/videoarea.h"
#include "components/statusbar.h"
#include "components/soundcontrol.h"
#include "gtk_extra/gtk_extra.h"
#include "resources/stock_icons.h"

static GtkWidget* create_player_tools (void);

/* Callbacks */

static void cb_clicked_play_button (GtkButton *button, gpointer user_data);
static void cb_clicked_button (GtkButton *button, gpointer user_data);

static gboolean cb_change_value_hscale (GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data);

GtkWidget*
create_main_window (void)
{

  GtkWidget *vbox;
  intf_sys_t *p_sys;
  
  p_sys = VLC_INTF_SYS;
  vbox = gtk_vbox_new (FALSE, 0);  
  
  p_sys->p_hscale = gtk_hscale_new_with_range (0.0, 100, 1);
  gtk_scale_set_draw_value (GTK_SCALE (p_sys->p_hscale), FALSE);  
  g_signal_connect (G_OBJECT (p_sys->p_hscale), "change-value", G_CALLBACK (cb_change_value_hscale), FALSE);

  p_sys->p_video = vlc_video_area_new ();

  gtk_box_pack_start (GTK_BOX (vbox), vlc_menu_bar_new (), FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), p_sys->p_video, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), p_sys->p_hscale, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), create_player_tools (), FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), vlc_status_bar_new (), FALSE, FALSE, 0);

  return vbox;

}

static GtkWidget*
create_player_tools (void)
{

  GtkWidget *main_vbox;
  GtkWidget *main_hbox;
  GtkWidget *alignment;
  GtkWidget *hbox;
  GtkWidget *button;
  GtkWidget *image;
  GtkTooltips *tooltips;
  
  intf_sys_t *p_sys;
  
  p_sys = VLC_INTF_SYS;
  
  main_vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 8);

  main_hbox = gtk_hbox_new (FALSE, 0);

  tooltips = gtk_tooltips_new ();

  /* Play button */
  p_sys->play_button = gtk_button_new ();
  
  image = gtk_image_new_from_vlc_stock ((playlist_IsEmpty(VLC_PLAYLIST)) ? VLC_STOCK_MEDIA_PLAY : VLC_STOCK_MEDIA_PAUSE,\
                                        VLC_ICON_SIZE_BUTTON);
  
  gtk_button_set_image (GTK_BUTTON (p_sys->play_button), image);
  gtk_button_set_relief (GTK_BUTTON (p_sys->play_button), GTK_RELIEF_NONE);
  gtk_tooltips_set_tip (tooltips, p_sys->play_button, _("Play"), NULL);
  g_signal_connect (G_OBJECT (p_sys->play_button), "clicked", G_CALLBACK (cb_clicked_play_button), NULL);
  gtk_box_pack_start (GTK_BOX (main_hbox), p_sys->play_button, FALSE, FALSE, 0);
  
  alignment = gtk_alignment_new (1.0, 1.0, 0.0, 0.0);
  
  gtk_box_pack_start (GTK_BOX(main_hbox), alignment, FALSE, FALSE, 8);

  /* Previous, Stop, Next buttons */ 
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_container_add (GTK_CONTAINER (alignment), hbox);

  button = gtk_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_MEDIA_PREVIOUS, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_tooltips_set_tip (tooltips, button, _("Previous"), NULL);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_button), (gpointer)ACTION_PREVIOUS);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  button = gtk_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_MEDIA_STOP, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_tooltips_set_tip (tooltips, button, _("Stop"), NULL);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_button), (gpointer)ACTION_STOP);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  button = gtk_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_MEDIA_NEXT, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_tooltips_set_tip (tooltips, button, _("Next"), NULL);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_button), (gpointer)ACTION_NEXT);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  
  gtk_box_pack_start (GTK_BOX (hbox), gtk_alignment_new (0.0, 0.0, 0.0, 0.0), FALSE, FALSE, 16);
  
  /* Fullscreen, playlist, equalizer buttons */
  button = gtk_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_FULLSCREEN, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_tooltips_set_tip (tooltips, button, _("Fullscreen"), NULL);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_button), (gpointer)ACTION_FULLSCREEN);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  button = gtk_toggle_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_LIST, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  button = gtk_button_new ();
  image = gtk_image_new_from_vlc_stock (VLC_STOCK_EQUALIZER, VLC_ICON_SIZE_MENU);
  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);  
  
  gtk_box_pack_start (GTK_BOX (hbox), vlc_sound_control_new (), FALSE, FALSE, 0);  
  
  gtk_box_pack_start (GTK_BOX (main_vbox), main_hbox, FALSE, FALSE, 0);

  return main_vbox;

}

static gboolean
cb_change_value_hscale (GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data)
{

  return FALSE;

}

static void
cb_clicked_play_button (GtkButton *button, gpointer user_data)
{
  
  if (is_file_in_playlist ())
   {    
    if (is_playing ())
     {
      pause ();
     }
    else
     {
      play ();
     }   
   }
  else
   {
    show_open_file ();
   }
     
}

static void
cb_clicked_button (GtkButton *button, gpointer user_data)
{

  int i;

  i = (int)user_data;

  switch (i)
   {   
    case ACTION_PLAY:    
     play ();     
    break;
    
    case ACTION_PAUSE:    
     pause ();     
    break;
    
    case ACTION_STOP:
     stop ();
    break;
    
    case ACTION_PREVIOUS:
     previous ();
    break;
    
    case ACTION_NEXT:
     next ();
    break;
    
    case ACTION_FULLSCREEN:
     fullscreen ();
    break;
    
    case ACTION_OPEN_FILE:
     show_open_file ();
    break;
   }

  (void)button;

}

